package cn.edu.jxau.test;

import java.util.Objects;
import java.util.concurrent.ExecutionException;

import cn.edu.jxau.util.LinkedStack;
import cn.edu.jxau.util.Stack;

public class Main {

	public static void main(String[] args) throws InterruptedException, ExecutionException {

		int[] arr1 = new int[] { 1, 2, 3, 4, 5 };
		int[] arr2 = new int[] { 4, 5, 3, 2, 1 };
		System.out.println(isPopOrder(arr1, arr2));
	}

	public static boolean isPopOrder(int[] arr1, int[] arr2) {

		Objects.requireNonNull(arr1, "参数arr1不能为null");
		Objects.requireNonNull(arr2, "参数arr2不能为null");
		if (arr1.length == 0) {
			throw new IllegalArgumentException("空数组");
		}
		if (arr1.length != arr2.length) {
			return false;
		}

		Stack<Integer> stack = new LinkedStack<>();
		int index = 0;
		for (int i = 0; i < arr1.length; i++) {
			stack.push(arr1[i]);
			if (stack.peek() == arr2[index]) {
				stack.pop();
				index++;
				while (!stack.isEmpty() && stack.peek() == arr2[index]) {
					stack.pop();
					index++;
				}
			}
		}
		while (index < arr2.length) {
			if (stack.pop() != arr2[index++]) {
				return false;
			}
		}
		return true;
	}
}